//
//  LLDropDownButton.swift
//  LaiLang
//
//  Created by David on 2017/2/17.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class LLDropDownButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        _font = UIFont.systemFont(ofSize: 14)
        foreground = UIColor.black
        layer.cornerRadius = frame.height / 4
        layer.borderWidth = 1
        
    }
    
    private var _font: UIFont!
    var normalFont: UIFont{
        get{
            return _font
        }
        set(f){
            _font = f
            setTitle(titleLabel?.text, for: .normal)
        }
    }
    
    private var _foreground: UIColor!
    var foreground: UIColor{
        get{
            return _foreground
        }
        set(f){
            _foreground = f
            layer.borderColor = f.cgColor
            setTitle(titleLabel?.text, for: .normal)
        }
    }

//    private var iconAdded = false
    
    override func setTitle(_ title: String?, for state: UIControlState) {
        if title == nil {
            return
        }
        if title!.characters.last == "\u{e61f}" {
            let width = (title! as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: frame.height), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: _font], context: nil).width
            
            frame = CGRect(origin: frame.origin, size: CGSize(width: width + 16, height: frame.height))
            
            let attrStr = NSMutableAttributedString(string: title!)
            attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: _font.pointSize)!, range: NSMakeRange(title!.characters.count - 1, 1))
            attrStr.addAttribute(NSFontAttributeName, value: _font, range: NSMakeRange(0, title!.characters.count - 1))
            attrStr.addAttribute(NSForegroundColorAttributeName, value: _foreground, range: NSMakeRange(0, title!.characters.count))
            setAttributedTitle(attrStr, for: state)
        }else{
            let stringAddingIcon = "\(title!)\u{e61f}"
            let width = (stringAddingIcon as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: frame.height), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: _font], context: nil).width
            
            frame = CGRect(origin: frame.origin, size: CGSize(width: width + 16, height: frame.height))
            
            let attrStr = NSMutableAttributedString(string: stringAddingIcon)
            attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: _font.pointSize)!, range: NSMakeRange(title!.characters.count, 1))
            attrStr.addAttribute(NSFontAttributeName, value: _font, range: NSMakeRange(0, title!.characters.count))
            attrStr.addAttribute(NSForegroundColorAttributeName, value: _foreground, range: NSMakeRange(0, stringAddingIcon.characters.count))
            setAttributedTitle(attrStr, for: state)
        }
//        var stringAddingIcon = title!
//        if iconAdded{
//            
//            
//        }else{
//            
////            iconAdded = true
//        }
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
